/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyectoTiberio.login;

import com.proyectoTiberio.entity.BaseEntity;
import com.proyectoTiberio.entity.PlayerInfoEntity;
import com.proyectoTiberio.entity.ServerInfoEntity;
import com.proyectoTiberio.portal.PortalForm;
import com.proyectoTiberio.servicio.ServicioHttp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author Chema
 */

@Controller
public class LoginController {
    
    @RequestMapping(value="index.do", method = RequestMethod.GET) 
    public String displayLogin(ModelMap model)
    {
        model.addAttribute("loginForm", new LoginForm());
        return "login/login"; 
    } 

    
    @RequestMapping(value="login.do", method= RequestMethod.POST)
    public String login(ModelMap model,@ModelAttribute("loginForm") LoginForm loginForm,
			BindingResult result, SessionStatus status) 
    {
        //map.addAttribute("hello", "Hello Spring from Netbeans!!");
        ServicioHttp servHttp = new ServicioHttp();
        ServerInfoEntity serverInfo;
        if("1".equals(loginForm.getNombre())) 
            serverInfo = servHttp.conectar(1);
        else
            serverInfo = servHttp.conectar(2);
        
        if (serverInfo != null)
        {
            PlayerInfoEntity playerInfo = servHttp.getPlayerInfo();
            
            
            
            if(playerInfo != null)
            {
                
                
                PortalForm portalForm = LoginMapper.toPortalForm(serverInfo,playerInfo);
                model.addAttribute("portalForm", portalForm);
                return "portal";
                
            }
            else
                //TODO: mostrar pantalla error
                return "";
            
          
        }
        else
            //TODO: mostrar pantalla error
        
        return "";
        
        
    }
    
 /*@ModelAttribute("loginForm")
 public LoginForm getloginForm() {
  return new LoginForm();
 }
 
 
 
@RequestMapping(method=RequestMethod.GET)
public void listArticles(ModelMap model){
   
    model.put("loginForm",new LoginForm());
}

*/
}
